using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;


namespace Mud.Common.GUI
{
	/// <summary>
	/// Text box which can be used as log sink for the Logger class.
	/// </summary>
    public class LogTextBox : UserControl, ILogSink
	{
		#region ILogSink Members

		public void Log(LogType logType, string source, string message) {
			if ((logType.Severity & _filter) != logType.Severity)
				return;
			if (this.InvokeRequired) {
                this.BeginInvoke(new AsyncLog(AppendToLog), logType, source, message);
			} else {
                AppendToLog(logType, source, message);
			}
		}

		public void OnSinkAdded() {
		}

		public void OnSinkRemoved() {
		}

        public LogType.LogSeverity Filter
        {
            get { return _filter; }
            set {
                if (_filter != value)
                {
                    _filter = value;
                    SetFilterButtons();
                }
            }
        }
        private LogType.LogSeverity _filter = LogType.LogSeverity.All;

		#endregion

		private void AppendToLog(LogType logType, string source, string message) {
            _txtLog.AppendText(Logger.Format(logType, source, message));
            _txtLog.AppendText(Environment.NewLine);
            _txtLog.ScrollToCaret();
        }

		private delegate void AsyncLog(LogType logType, string source, string message);


		private TextBox _txtLog = new TextBox();
        private ToolStrip _toolStrip = new ToolStrip();
        private ToolStripLabel _lblFilter = new ToolStripLabel("Filters:");
        private ToolStripButton _btnClear = new ToolStripButton("Clear");

		public LogTextBox() {
            _toolStrip.Items.Add(_lblFilter);
            CreateFilterButton("Info").Checked = true;
            CreateFilterButton("Warning").Checked = true;
            CreateFilterButton("Error").Checked = true;
            CreateFilterButton("Debug 1");
            CreateFilterButton("Debug 2");
            _toolStrip.Items.Add(_btnClear);
       
            _toolStrip.Dock = DockStyle.Top;
            SetFilterButtons();

            _btnClear.Click += new EventHandler(_btnClear_Click);

            _txtLog.ReadOnly = true;
            _txtLog.BackColor = Color.Black;
            _txtLog.ForeColor = Color.GreenYellow;
            _txtLog.Font = new Font("Courier New", 8.0f, FontStyle.Bold, GraphicsUnit.Point);
			_txtLog.Multiline = true;
			_txtLog.ScrollBars = ScrollBars.Both;
            _txtLog.Dock = DockStyle.Fill;

            this.Controls.Add(_txtLog);
            this.Controls.Add(_toolStrip);
            
		}

        void _btnClear_Click(object sender, EventArgs e)
        {
            _txtLog.Clear();
        }

        private ToolStripButton CreateFilterButton(string buttonText)
        {
            ToolStripButton btn = new ToolStripButton();
            btn.Text = buttonText;
            btn.Name = buttonText;
            btn.Click += new EventHandler(ToolStripButtonClicked);
            btn.CheckOnClick = true;
            btn.Checked = false;
            _toolStrip.Items.Add(btn);
            return btn;
        }

        void ToolStripButtonClicked(object sender, EventArgs e)
        {
            SetFilterButtons();
        }


        private void SetFilterButtons()
        {
            LogType.LogSeverity severity = LogType.LogSeverity.None;
            if (((ToolStripButton)_toolStrip.Items["Info"]).Checked)
                severity |= LogType.LogSeverity.Information;

            if (((ToolStripButton)_toolStrip.Items["Warning"]).Checked)
                severity |= LogType.LogSeverity.Warning;

            if (((ToolStripButton)_toolStrip.Items["Error"]).Checked)
                severity |= LogType.LogSeverity.Error;

            if (((ToolStripButton)_toolStrip.Items["Debug 1"]).Checked)
                severity |= LogType.LogSeverity.Debug1;

            if (((ToolStripButton)_toolStrip.Items["Debug 2"]).Checked)
                severity |= LogType.LogSeverity.Debug2;
            this.Filter = severity;
        }

	}
}
